package com.example.yichuangyun.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.PopupCompanyAreaLayoutBinding;
import com.example.yichuangyun.databinding.PopupCompanyManageLayoutBinding;
import com.example.yichuangyun.model.company.ScreenModel;
import com.example.yichuangyun.ui.adapter.EmptyRecyclerCommonAdapter;
import com.example.yichuangyun.ui.adapter.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 经营状态排序
 */
@SuppressLint("NotifyDataSetChanged")
public abstract class ManagementStatusScreenPopupWindow extends PopupWindow {

    private final List<ScreenModel> data = new ArrayList<>();
    private final EmptyRecyclerCommonAdapter<ScreenModel> adapter;
    private final List<Integer> statusCode = new ArrayList<>();

    public ManagementStatusScreenPopupWindow(Context context) {
        PopupCompanyManageLayoutBinding binding = PopupCompanyManageLayoutBinding.inflate(LayoutInflater.from(context));
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow的焦点
        this.setFocusable(true);
        setContentView(binding.getRoot());
        binding.recyclerView.setLayoutManager(new GridLayoutManager(context, 5));
        data.add(new ScreenModel(context.getString(R.string.cun_xu), 1));
        data.add(new ScreenModel(context.getString(R.string.zhu_xiao), 2));
        data.add(new ScreenModel(context.getString(R.string.diao_xiao), 3));
        data.add(new ScreenModel(context.getString(R.string.che_xiao), 4));
        data.add(new ScreenModel(context.getString(R.string.qian_chu), 5));
        data.add(new ScreenModel(context.getString(R.string.she_li_zhong), 6));
        data.add(new ScreenModel(context.getString(R.string.qing_suan_zhong), 7));
        data.add(new ScreenModel(context.getString(R.string.ting_ye), 8));
        data.add(new ScreenModel(context.getString(R.string.qi_ta), 9));

        adapter = new EmptyRecyclerCommonAdapter<ScreenModel>(context, R.layout.item_company_area, data) {
            @Override
            public void convert(RecyclerViewHolder holder, ScreenModel model, int position) {
                holder.setText(R.id.tv_item, model.getName());
                holder.itemView.setBackgroundResource(model.isSelect() ? R.drawable.shape_label_bg_sel : R.drawable.shape_label_bg_def);
                holder.setTextColor(R.id.tv_item, model.isSelect() ? context.getColor(R.color.color_426FFF) : context.getColor(R.color.color_4A4A4A));
            }

            @Override
            public void bindClick(RecyclerViewHolder holder, ScreenModel model, int position) {
                super.bindClick(holder, model, position);
                holder.setOnClickListener(R.id.tv_item, view -> {
                    model.setSelect(!model.isSelect());
                    notifyDataSetChanged();
                });
            }
        };
        binding.recyclerView.setAdapter(adapter);
        binding.includeBtn.view.setOnClickListener(v -> {
//            reset();
            dismiss();
        });
        binding.includeBtn.tvReset.setOnClickListener(v -> {
            reset();
            confirm(statusCode, null);
            dismiss();
        });
        binding.includeBtn.tvConfirm.setOnClickListener(v -> {
            statusCode.clear();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).isSelect()) {
                    statusCode.add(data.get(i).getCode());
                    builder.append(data.get(i).getName()).append(",");
                }
            }
            confirm(statusCode, builder.toString().length() > 0 ? builder.substring(0, builder.toString().length() - 1) : "");
            dismiss();
        });
    }


    public void reset() {
        for (ScreenModel screenModel : data) {
            screenModel.setSelect(false);
        }
        adapter.notifyDataSetChanged();
        statusCode.clear();
    }

    public abstract void confirm(List<Integer> statusCode, String status);

}
